window.chatmessagedb = {
	insert: (msg, call) => {
		if (msg == null) {
			return;
		}
		// console.log(JSON.stringify(msg))
		let message_id = msg.messageId == null ? -1 : msg.messageId;
		let create_time = msg.createTime;
		let message = msg.message;
		let status = msg.status;
		let receive_user_id = msg.receiveUserId;
		let send_user_id = msg.sendUserId;
		let type = msg.type;
		let is_myself = msg.isMyself;
		let userId = app.getUserId();
		let friendId = send_user_id == userId ? receive_user_id : send_user_id;
		let sql =
			'insert into t_chat_message (message_id, create_time, message, status, receive_user_id, send_user_id, type, is_myself, user_id, friend_id) ' +
			'values (' +
			message_id + ', "' + create_time + '", "' + message + '", ' + status + ', ' + receive_user_id + ', ' +
			send_user_id + ', ' + type +
			', ' + is_myself + ', ' + userId + ', ' + friendId +
			' )'
		if (message_id != -1) {
			chatmessagedb.delete({
				message_id
			}, (data) => {
				db.excuteSql(sql, call);
			});
		} else{
			db.excuteSql(sql, call);
		}
		
	},
	delete: (params, call) => {
		if (!params) {
			return;
		}
		let id = params.id;
		let message_id = params.message_id;
		if (!id && !message_id) {
			return;
		}
		let sql = 'delete from t_chat_message where 1=1 ';
		if (id) {
			sql += ' and id = ' + id;
		}
		if (message_id) {
			sql += ' and message_id = ' + message_id;
		}
		db.excuteSql(sql, call);
	},
	deleteByMapping: (params, call) => {
		if (!params) {
			return;
		}
		let userId = params.userId;
		let friendId = params.friendId;
		if (userId == null || friendId == null) {
			return;
		}
		let sql = 'delete from t_chat_message where 1=1 and user_id=' + userId + ' and friend_id=' + friendId;
		db.excuteSql(sql, call);
	},
	update: (msg, call) => {
		if (msg == null) {
			return;
		}
		let id = msg.id;
		let status = msg.status;
		let type = msg.type;
		let sql = 'update t_chat_message set status=' + status + ', type=' + type + ' where id=' + id;
		db.excuteSql(sql, call);
	},
	updateStatus: (params, call) => {
		if (params == null) {
			return;
		}
		let userId = params.userId;
		let friendId = params.friendId;
		let status = params.status;
		let sql = 'update t_chat_message set status=' + status + ' where user_id=' + userId + ' and friend_id=' + friendId;
		db.excuteSql(sql, call);
	},
	select: (params, call) => {
		let sql = ' select * from t_chat_message ';
		if (params != null) {
			let userId = params.userId;
			let friendId = params.friendId;
			if (userId != null & friendId != null) {
				sql += ' where 1=1 and user_id=' + userId + ' and friend_id=' + friendId;
			}
			let id = params.id;
			if (id) {
				sql += 'and id=' + id;
			}
		}
		params.sort = ' order by create_time asc '
		db.selectSQL(sql, call, params);
	},
	selectCount: (params, call) => {
		let sql = ' select count(1) as count from t_chat_message where 1=1 ';
		if (params != null) {
			let userId = params.userId;
			let friendId = params.friendId;
			if (userId != null) {
				sql += ' and user_id=' + userId ;
			}
			if (friendId != null) {
				sql += ' and friend_id=' + friendId;
			}
			let status = params.status;
			if (status) {
				sql += ' and status=' + status;
			}
			let id = params.id;
			if (id) {
				sql += ' and id=' + id;
			}
			let message_id = params.message_id;
			if (message_id) {
				sql += ' and message_id=' + message_id;
			}
		}
		db.selectSQL(sql, call, params);
	}
}
